package com.luck.easyfoodbatch.ui;

import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.google.gson.Gson;
import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.adapter.CommentEvaluateAdapter;
import com.luck.easyfoodbatch.custom.RoundedImageView;
import com.luck.easyfoodbatch.entity.Comment;
import com.luck.easyfoodbatch.mvp.model.Collect;
import com.luck.easyfoodbatch.mvp.model.MyOrdersM;
import com.luck.easyfoodbatch.mvp.order.AddEvaluateModel;
import com.luck.easyfoodbatch.mvp.order.AddEvaluatePresenter;
import com.luck.easyfoodbatch.mvp.order.AddEvaluateView;
import com.luck.easyfoodbatch.util.HttpUtil;
import com.luck.easyfoodbatch.util.T;
import com.luck.easyfoodbatch.util.Tools;
import com.luck.easyfoodbatch.util.Utils;
import com.luck.easyfoodbatch.util.XLHRatingBar;
import com.luck.easyrecyclerview.EasyRecyclerView;
import com.luck.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.luck.easyrecyclerview.decoration.RecycleViewDivider;
import com.yalantis.ucrop.entity.LocalMedia;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CommentActivity extends BaseActivity implements AddEvaluateView {
    private String order_id = "";
    private AddEvaluateModel evaluateModel;
    @BindView(R.id.recyclerView)
    EasyRecyclerView recyclerView;

    private XLHRatingBar rating_shop;
    private TextView tv_shop_name;
    private RoundedImageView riv_avatar;
    private CommentEvaluateAdapter adapter;
    private List<MyOrdersM.DataBean.ModelsBean.OrderInfoBean> orderInfo = new ArrayList<>();
    private Gson gson;
    private String shop_name = "", shop_avatar = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comment, "发表评论", "发布", MODE_BACK_NAVIGATION);
        ButterKnife.bind(this);
        gson = new Gson();
        order_id = getIntent().getStringExtra("order_id");
        shop_name = getIntent().getStringExtra("shop_name");
        shop_avatar = getIntent().getStringExtra("shop_avatar");
        orderInfo = (List<MyOrdersM.DataBean.ModelsBean.OrderInfoBean>) getIntent().getSerializableExtra("orderInfo");
        if (orderInfo == null) {
            orderInfo = new ArrayList<>();
        }
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.addItemDecoration(new RecycleViewDivider(
                mContext, LinearLayoutManager.HORIZONTAL, Utils.dip2px(mContext, 0.5F), getResources().getColor(R.color.line_color)));
        recyclerView.setLayoutManager(manager);
        adapter = new CommentEvaluateAdapter(mContext);
        adapter.addFooter(new RecyclerArrayAdapter.ItemView() {
            @Override
            public View onCreateView(ViewGroup parent) {
                View evaluate_footer = LayoutInflater.from(mContext).inflate(R.layout.evaluate_footer, null);
                riv_avatar = (RoundedImageView) evaluate_footer.findViewById(R.id.riv_avatar);
                tv_shop_name = (TextView) evaluate_footer.findViewById(R.id.tv_shop_name);
                rating_shop = (XLHRatingBar) evaluate_footer.findViewById(R.id.rating_shop);
                if (!Tools.isNull(shop_name)) {
                    tv_shop_name.setText(shop_name);
                } else {
                    tv_shop_name.setText("");
                }
                if (!Tools.isNull(shop_avatar)) {
                    Glide.with(getApplicationContext())
                            .load(HttpUtil.IMG_BASE_URL + shop_avatar)
                            .centerCrop()
                            .placeholder(R.mipmap.loading_4b3)
                            .diskCacheStrategy(DiskCacheStrategy.ALL)
                            .into(riv_avatar);
                } else {
                    riv_avatar.setImageResource(R.mipmap.loading_4b3);
                }
                return evaluate_footer;
            }

            @Override
            public void onBindView(View headerView) {

            }
        });
        adapter.addAll(orderInfo);
        recyclerView.setAdapter(adapter);
    }

    @Override
    public void rightClick() {
        super.rightClick();
        showProgressDialog(getString(R.string.app_loading));
        evaluateModel = new AddEvaluatePresenter(this, mContext);
        List<Comment> comments = new ArrayList<>();
        for (MyOrdersM.DataBean.ModelsBean.OrderInfoBean item : orderInfo) {
            Comment comment = new Comment();
            comment.setInfo_id(item.getInfo_id() + "");
            comment.setG_star(item.getG_star());
            comment.setContent(item.getContent());
            comment.setGid(item.getGid()+"");
            List<LocalMedia> photoInfos = item.getPhotoInfos();
            comment.setPhotoInfos(photoInfos);
            comments.add(comment);
        }
        String toJson = gson.toJson(comments);
        int countSelected = rating_shop.getCountSelected();
        evaluateModel.requestAddEvaluate(order_id, countSelected + "", toJson);
    }

    @Override
    public void addEvaluateSuccess(Collect model) {
        if (model != null) {
            if (model.getStatus() == HttpUtil.RESULT_OK) {
                sendBroadcast("action.app.refresh.evaluate");
                finish();
            } else {
                T.showToast(mContext, model.getMsg());
            }
        }
    }

    @Override
    public void httpStatusError() {

    }

    @Override
    public void hideLoading() {
        hideShowProgress();
    }
}
